package com.ml.carManager.ui.diver;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;


import com.hjq.toast.ToastUtils;
import com.jaeger.library.StatusBarUtil;
import com.ml.carManager.R;
import com.ml.carManager.net.NetProxyListener;
import com.ml.carManager.utils.auth.AuthManager;

import butterknife.BindView;
import custom.base.entity.auth.LoginUser;
import custom.base.entity.base.BaseResponse;
import custom.base.ui.activity.BaseActivity;
import retrofit2.Call;

public class MainDivierActivity extends BaseActivity {
    @BindView(R.id.act_main_home)
    LinearLayoutCompat llHome;
    @BindView(R.id.act_main_home_text)
    TextView tvHome;
    @BindView(R.id.act_main_workbench)
    LinearLayoutCompat llWorkbench;
    @BindView(R.id.act_main_workbench_text)
    TextView tvWorkbench;
    @BindView(R.id.act_main_kuaqu)
    LinearLayoutCompat llKuaqu;
    @BindView(R.id.act_main_kuaqu_text)
    TextView tvKuaqu;
    @BindView(R.id.act_main_my)
    LinearLayoutCompat llMy;
    @BindView(R.id.act_main_my_text)
    TextView tvMy;


    FragmentHome fragmentHome;
    FragmentWorkbench fragmentWorkbench;
    FragmentKuaqu fragmentKuaqu;
    FragmentMy fragmentMy;

    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;

    @Override
    public boolean initStatusBar() {
        StatusBarUtil.setColor(getActivity(), ContextCompat.getColor(getActivity(), R.color.app_main_color), 0);
        return false;
    }

    @Override
    public int getLayoutID() {
        return R.layout.act_main;
    }

    @Override
    protected void onResume() {
        super.onResume();
//        refreshToken();
    }


    @Override
    public void befordCreateView(@NonNull Bundle savedInstanceState) {
        super.befordCreateView(savedInstanceState);
    }

    @Override
    public void initView(@NonNull Bundle savedInstanceState) {
        super.initView(savedInstanceState);

        initTab();
    }

    @Override
    public void initObject(@NonNull Bundle savedInstanceState) {
        super.initObject(savedInstanceState);
    }

    @Override
    public void initData(@NonNull Bundle savedInstanceState) {
        super.initData(savedInstanceState);

    }

    @Override
    public void initListener() {
        llHome.setOnClickListener(this);
        llWorkbench.setOnClickListener(this);
        llKuaqu.setOnClickListener(this);
        llMy.setOnClickListener(this);
    }


    @Override
    public void onClick(View v, int clickID) {
        if (clickID == llHome.getId()) {
            tabMap();
        } else if (clickID == llWorkbench.getId()) {
            tabModule();
        } else if (clickID == llKuaqu.getId()) {
            tabMessage();
        } else if (clickID == llMy.getId()) {
            tabMy();
        }
    }


    /**
     * 初始化tabs
     */
    private void initTab() {
        fragmentManager = getSupportFragmentManager();
        transaction = fragmentManager.beginTransaction();

        //程序进入时默认全界面生成
        tabMy();
        tabMessage();
        tabModule();
        tabMap();
    }

    private void tabMap() {
        transaction = fragmentManager.beginTransaction();
        hideAll(transaction);
        if (fragmentHome == null) {
            fragmentHome = new FragmentHome();
            transaction.add(R.id.act_main_tab_frameLayout, fragmentHome);
        } else {
            transaction.show(fragmentHome);
            fragmentHome.onFragmentResume();
        }
        transaction.commit();
        selectTab(0);
    }


    private void tabModule() {
        transaction = fragmentManager.beginTransaction();
        hideAll(transaction);
        if (fragmentWorkbench == null) {
            fragmentWorkbench = new FragmentWorkbench();
            transaction.add(R.id.act_main_tab_frameLayout, fragmentWorkbench);
        } else {
            transaction.show(fragmentWorkbench);
            fragmentWorkbench.onFragmentResume();
        }
        transaction.commit();
        selectTab(1);
    }

    private void tabMessage() {
        transaction = fragmentManager.beginTransaction();
        hideAll(transaction);
        if (fragmentKuaqu == null) {
            fragmentKuaqu = new FragmentKuaqu();
            transaction.add(R.id.act_main_tab_frameLayout, fragmentKuaqu);
        } else {
            transaction.show(fragmentKuaqu);
            fragmentKuaqu.onFragmentResume();
        }
        transaction.commit();
        selectTab(2);
    }

    private void tabMy() {
        transaction = fragmentManager.beginTransaction();
        hideAll(transaction);
        if (fragmentMy == null) {
            fragmentMy = new FragmentMy();
            transaction.add(R.id.act_main_tab_frameLayout, fragmentMy);
        } else {
            transaction.show(fragmentMy);
            fragmentMy.onFragmentResume();
        }
        transaction.commit();
        selectTab(3);
    }

    /**
     * 隐藏所有碎片
     */
    private void hideAll(FragmentTransaction transaction) {
        if (fragmentHome != null)
            transaction.hide(fragmentHome);
        if (fragmentWorkbench != null)
            transaction.hide(fragmentWorkbench);
        if (fragmentKuaqu != null)
            transaction.hide(fragmentKuaqu);
        if (fragmentMy != null)
            transaction.hide(fragmentMy);
    }

    /**
     * 不选中所有的tab
     */
    private void unselectAllTab() {
        llHome.setSelected(false);
        llWorkbench.setSelected(false);
        llKuaqu.setSelected(false);
        llMy.setSelected(false);

        tvHome.setTextColor(ContextCompat.getColor(this, R.color.app_title_main));
        tvWorkbench.setTextColor(ContextCompat.getColor(this, R.color.app_title_main));
        tvKuaqu.setTextColor(ContextCompat.getColor(this, R.color.app_title_main));
        tvMy.setTextColor(ContextCompat.getColor(this, R.color.app_title_main));
    }

    /**
     * 选中tab
     */
    private void selectTab(int index) {
        unselectAllTab();

        switch (index) {
            case 0:
                llHome.setSelected(true);

                tvHome.setTextColor(ContextCompat.getColor(this, R.color.app_main_color));
                break;
            case 1:
                llWorkbench.setSelected(true);

                tvWorkbench.setTextColor(ContextCompat.getColor(this, R.color.app_main_color));
                break;
            case 2:
                llKuaqu.setSelected(true);

                tvKuaqu.setTextColor(ContextCompat.getColor(this, R.color.app_main_color));
                break;
            case 3:
                llMy.setSelected(true);

                tvMy.setTextColor(ContextCompat.getColor(this, R.color.app_main_color));
                break;

        }
    }


    public void requestPermission() {
//        new RxPermissions(getActivity())
//                .request(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE)
//                .subscribe(new Consumer<Boolean>() {
//                    @Override
//                    public void accept(@NonNull Boolean aBoolean) throws Exception {
//                        if (!aBoolean) {
//                            SimpleHintDialog simpleHintDialog = new SimpleHintDialog(getActivity());
//                            simpleHintDialog.setContentString("被禁止权限后无法正常使用软件");
//                            simpleHintDialog.setCancleBtnTxt("退出");
//                            simpleHintDialog.setEnterBtnTxt("重新获取");
//                            simpleHintDialog.setOnCancelClickListener(new OnCancelClickListener() {
//                                @Override
//                                public void onCancel() {
//                                    AppManager.getInstance().finishAllActivity();
//                                }
//                            });
//                            simpleHintDialog.setOnEnterClickListener(new OnEnterClickListener() {
//                                @Override
//                                public void onEnter() {
//                                    requestPermission();
//                                }
//                            });
//                            simpleHintDialog.show();
//                        } else {
//                        }
//                    }
//                });
    }

    private void refreshToken() {
        Call<BaseResponse<LoginUser>> call = appApi.refreshToken(AuthManager.getInstance(getApplicationContext()).getLoginUser().getRefreshToken());
        call.enqueue(new NetProxyListener<LoginUser>(getALiveNetCallback()) {
            @Override
            protected boolean dealNullResponseData() {
                return true;
            }

            @Override
            public void onResponseSuccess(Call call, BaseResponse<LoginUser> response) {
                LoginUser loginUserNew = response.getData();
                LoginUser loginUser = AuthManager.getInstance(getApplicationContext()).getLoginUser();
                loginUser.setAccessToken(loginUserNew.getAccessToken());
                loginUser.setRefreshToken(loginUserNew.getRefreshToken());

                AuthManager.getInstance(getApplicationContext()).updateUserInfo(loginUser);
            }

            @Override
            public void onResponseError(Call call, Throwable t) {
                super.onResponseError(call, t);
            }

            @Override
            public void onResponseCodeError(Call call, BaseResponse<LoginUser> response) {
                super.onResponseCodeError(call, response);
            }
        });

    }

    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                ToastUtils.show("再按一次退出程序");
                exitTime = System.currentTimeMillis();
            } else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


}
